package com.player.thirdGroup.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;

public class LoadUtil {

	//上传
	public static String ScPhoto(MultipartFile loadimg){
		try {
			//存储位置
			String path = "G:\\A各种项目\\ShiXun\\threeGroup3\\threeGroup3\\src\\main\\resources\\static\\img";
			//获取上传的文件名称
			String filename = loadimg.getOriginalFilename();
			//拼接文件地址
			String filepath=path+"//"+filename;
			File file = new File(filepath);
			//将上传文件保存到目标文件目录
			loadimg.transferTo(file);
			return filepath;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	//����ͼƬ
	public static void ShowPhoto(String photo,HttpServletResponse response){
		 try {
			//��ȡͼƬ�ĵ�ǰ·��  �����
			 FileInputStream fis = new FileInputStream(photo);
			 //��response  ��ȡһ��д�������
			 ServletOutputStream os = response.getOutputStream();
			 //��߶�д���ٶ�
			 byte[] b=new byte[1024];
			//�߶���д
			 while(fis.read(b)!=-1){
				 os.write(b);
			 }
			 os.close();
			 fis.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	//����
	public static void downLode(String filepath,HttpServletRequest request, HttpServletResponse response){
		//�����ļ���MiMe����
		response.setContentType(request.getSession().getServletContext().getMimeType(filepath));
		//����content-dispsition
		response.setHeader("Content-Disposition", "attachment;filename="+filepath);
		//��ȡĿ���ļ���ͨ��response��Ŀ���ļ�д���ͻ�
		try {
			//��ȡ�ļ�
			InputStream  in = new FileInputStream(filepath);
			OutputStream out=response.getOutputStream();
			//д�ļ�
			byte[] b=new byte[1024];
			while(in.read(b)!=-1){
				out.write(b);
			}
			in.close();
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
